package com.bds.btcdc.web.service;

import com.alibaba.fastjson.JSONObject;
import com.bds.btcdc.api.model.AccountInfo;
import com.bds.btcdc.api.model.Asset;
import com.bds.btcdc.api.model.AssetExchange;
import org.nutz.dao.Cnd;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by DongChenchen on 2018/5/16 0016
 */
@Service
public class AssetExchangeService extends BaseService<AssetExchange>{

    public Boolean updateAccountInfos(List<AccountInfo> infos){
        List<AssetExchange> ass=new ArrayList<>();
        for(AccountInfo info:infos){//遍历每个交易所
            for (Asset asset : info.getAssetList()) {
                try {
                    AssetExchange as = JSONObject.parseObject(JSONObject.toJSONString(asset), AssetExchange.class);
                    as.setUserId(info.getUserId());
                    as.setExchange(info.getExchange());
                    AssetExchange dbas = getAllByUserIdAndExchangeAndCoin(info.getUserId(), info.getExchange(), as.getCurrency());
                    if(dbas!=null){
                        as.setId(dbas.getId());//数据库中存在
                    }
                    ass.add(as);
                }catch (Exception e){
                    e.printStackTrace();
                    continue;
                }
            }
        }
        return updateOrInsertAll(ass);
    }
    public List<AssetExchange> getAllByUserId(Long userId){
        return dao.query(AssetExchange.class, Cnd.where("userId","=",userId));
    }
    public List<AccountInfo> getAccountInfos(Long userId){
        List<AssetExchange> exchAs=getAllByUserId(userId);
        Map<String,List<Asset>> exchanges=new HashMap<>();
        List<AccountInfo> infos=new ArrayList<>();
        for(AssetExchange exchA:exchAs){//遍历每个交易所
            String exchange = exchA.getExchange();
            List<Asset> assets=null;
            if(exchanges.containsKey(exchange)){
                assets = exchanges.get(exchange);
            }else{
                assets=new ArrayList<>();
            }
            Asset asset = JSONObject.parseObject(JSONObject.toJSONString(exchA), Asset.class);
            assets.add(asset);
            exchanges.put(exchange,assets);
        }
        for(Map.Entry<String,List<Asset>> kv:exchanges.entrySet()){
            AccountInfo info=new AccountInfo();
                info.setAssetList(kv.getValue());
                info.setUserId(userId);
                info.setExchange(kv.getKey());
            infos.add(info);
        }
        return infos;
    }
    public AssetExchange getAllByUserIdAndExchangeAndCoin(Long userId,String exchange,String currency){
        Sql sql = Sqls.create("Select a.* from t_asset_exchange a where a.userId=@userId " +
                "and a.exchange=@exchange and a.currency=@currency");
        sql.params().set("userId", userId);
        sql.params().set("exchange", exchange);
        sql.params().set("currency", currency);
        sql.setCallback(Sqls.callback.entity());
        sql.setEntity(dao.getEntity(AssetExchange.class));
        dao.execute(sql);
        return sql.getObject(AssetExchange.class);
    }
}
